package com.kaibai.dao;

import com.kaibai.utils.JDBCUtil;

import java.lang.reflect.Field;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class BaseDao {


    //增删改
    public int update(Connection conn,String sql,Object... args){
        PreparedStatement ps = null;
        try {
            ps = conn.prepareStatement(sql);
            for(int i = 0;i < args.length;i++){
                ps.setObject(i+1,args[i]);
            }
            return ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtil.close(null,ps);
        }
        return 0;
    }

    //查单个
    public <T> T getInstance(Connection conn,Class<T> clazz,String sql,Object... args){
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(sql);
            for(int i = 0;i < args.length;i++){
                ps.setObject(i+1,args[i]);
            }
            rs = ps.executeQuery();
            ResultSetMetaData rsmd = rs.getMetaData();
            int columnCount = rsmd.getColumnCount();

            if(rs.next()) {
                T t = clazz.newInstance();
                for (int i = 0; i < columnCount; i++) {
                    Object columValue = rs.getObject(i+1);
                    String columLabel = rsmd.getColumnLabel(i+1);
                    Field field = clazz.getDeclaredField(columLabel);
                    field.setAccessible(true);
                    field.set(t,columValue);
                }
                return t;
            }
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtil.close(null,ps,rs);
        }
        return null;
    }

    //查多个
    public <T> List<T> getForList(Connection conn,Class<T> clazz,String sql,Object... args){
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(sql);
            for(int i = 0;i < args.length;i++){
                ps.setObject(i+1,args[i]);
            }
            rs = ps.executeQuery();
            ResultSetMetaData rsmd = rs.getMetaData();
            int columnCount = rsmd.getColumnCount();
            List<T> list = new ArrayList<>();
            while(rs.next()) {
                T t = clazz.newInstance();
                for (int i = 0; i < columnCount; i++) {
                    Object columValue = rs.getObject(i+1);
                    String columLabel = rsmd.getColumnLabel(i+1);
                    Field field = clazz.getDeclaredField(columLabel);
                    field.setAccessible(true);
                    field.set(t,columValue);
                }
                list.add(t);
            }
            return list;
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtil.close(null,ps,rs);
        }
        return null;
    }


    public <E> E getValue(Connection conn,String sql,Object... args){
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            ps = conn.prepareStatement(sql);
            for(int i = 0;i < args.length;i++){
                ps.setObject(i+1,args[i]);
            }
            rs = ps.executeQuery();

            if(rs.next()) {
                return (E) rs.getObject(1);
            }
        } catch (Exception throwables) {
            throwables.printStackTrace();
        }finally {
            JDBCUtil.close(null,ps,rs);
        }
        return null;
    }
}
